package tech.dongliang.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.dongliang.common.model.Tree;
import tech.dongliang.system.dao.SysMenuDao;
import tech.dongliang.system.dao.SysRoleMenuDao;
import tech.dongliang.system.dao.SysUserRoleDao;
import tech.dongliang.system.model.SysMenu;
import tech.dongliang.system.service.ISysMenuService;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xdzhang
 * @since 2019-01-18
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements ISysMenuService {
    @Autowired
    private SysRoleMenuDao roleMenuDao;
    @Autowired
    private SysUserRoleDao userRoleDao;

    @Override
    public List<SysMenu> list(QueryWrapper<SysMenu> queryWrapper) throws Exception {
        queryWrapper.orderByAsc("pid", "orderNum");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Tree<SysMenu> getTree() throws Exception {
        List<Tree<SysMenu>> trees = new ArrayList<>();
        List<SysMenu> sysMenus = baseMapper.selectList(null);
        for (SysMenu menu : sysMenus) {
            Tree<SysMenu> tree = new Tree<>();
            tree.setId(menu.getId().toString());
            tree.setParentId(menu.getPid().toString());
            tree.setText(menu.getName());
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<SysMenu> t = Tree.build(trees);
        return t;
    }

    @Override
    public Tree<SysMenu> getTree(Integer id) throws Exception {
        // 根据roleId查询权限
        List<SysMenu> menus = baseMapper.selectList(null);
        List<Integer> menuIds = roleMenuDao.listMenuIdByRoleId(id);
        for (SysMenu menu : menus) {
            if (menuIds.contains(menu.getPid())) {
                menuIds.remove(menu.getPid());
            }
        }
        List<Tree<SysMenu>> trees = new ArrayList<>();
        for (SysMenu sysMenuDO : menus) {
            Tree<SysMenu> tree = new Tree<>();
            tree.setId(sysMenuDO.getId().toString());
            tree.setParentId(sysMenuDO.getPid().toString());
            tree.setText(sysMenuDO.getName());
            Map<String, Object> state = new HashMap<>(16);
            if (menuIds.contains(sysMenuDO.getId())) {
                state.put("selected", true);
            } else {
                state.put("selected", false);
            }
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<SysMenu> t = Tree.build(trees);
        return t;
    }

    @Override
    public int childCount(Integer deptId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pid", deptId);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public Set<String> listPerms(Integer userId) throws Exception {
        List<Integer> roleIds = userRoleDao.getUserRoleId(userId);
        List<Integer> menuIds = roleMenuDao.listMenuIdByRoleIds(roleIds);
        return baseMapper.listPermsByIds(menuIds);
    }
}
